package com.c2.web.backend;

import com.c2.entity.EbComment;
import com.c2.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    /**
     * 将数据放到 model中，将页面放到view中
     * spring mvc---将model渲染到view中
     *
     * list ---分页请求数据
     */
    @RequestMapping("/list")
    public String list(Model model,@RequestParam(value = "page",defaultValue = "1") int psize,
                                   @RequestParam(value = "pageSize",defaultValue = "3") int pageSize) {
        System.out.println("start service.....;psize==>"+psize);
    //1.请求service层获取新闻的分页数据
        List<EbComment> commentList = commentService.listByPage(psize,pageSize);
        //2.传递model(数据)和view视图(jsp)
    int totalPage = commentService.getMaxPage(pageSize);
        //3传递model和view视图（jsp)
        model.addAttribute("commentList",commentList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute( "curPage",psize);
        return "manage/guestbook";//视图  通过manage/.jsp进行展示数据
    }
    @RequestMapping("/to_edit")
    public String toEdit(Model model,@RequestParam("ecId") int ecId){
        System.out.println("点击修改跳过来了，ecId="+ecId);
        //1.调用service层的方法查询点击的留言的所有信息
        EbComment ebComment = commentService.selectOne(ecId);
        //2.指定model和jsp
        model.addAttribute("ebComment",ebComment);
        return "manage/guestbook-modify";//manage/guestbook-modify.jsp
    }

    @RequestMapping("/editSubmit")
    public String editSubmit(EbComment ebComment) {
        //1.更新留言信息
        int result = commentService.updateById(ebComment);
        //2.跳转到更新结果页面
        if (result <= 0) {
            return "manage/manage-error-comment";
        }
        return "manage/manage-result-comment";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        //1.参数校验
        //2.执行删除（真实项目中delete from 表明 wehere id=?)
        int result = commentService.deleteByPk(id);
        if(result==0){
            return "manage/manage-error-comment";
        }
        return "manage/manage-result-comment";

    }
}